package com.yuantu.judiciary.model.word;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.xwpf.usermodel.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author kgwei
 * 文字段落
 */
@Data
@NoArgsConstructor
public class WordParagraph implements WordElement {

    private List<WordText> texts;

    /**
     * @see ParagraphAlignment
     * ParagraphAlignment.CENTER
     */
    private ParagraphAlignment alignment;

    /**
     * @see Borders
     * Borders.DOUBLE
     */
    private Borders borderBottom;

    /**
     * @see javafx.scene.text.TextAlignment
     * TextAlignment.TOP
     */
    private TextAlignment verticalAlignment;

    /**
     * 1.5，代表1.5倍行距
     */
    private Double spacingBetweenWidth;

    /**
     * @see LineSpacingRule
     * LineSpacingRule.EXACT
     */
    private LineSpacingRule spacingBetweenRule;

    /**
     *  200 TWIP（1 磅 = 20 TWIP, 200 TWIP = 10 磅）
     */
    private Integer spacingBefore;

    private Integer spacingAfter;

    /**
     * 首行缩进，字体size * 缩进几个字符 * 20
     */
    private Integer indentationFirstLine;

    private String style;

    /**
     * 第一个页面，两个标题
     * @param title1 。
     * @param title2 。
     * @return 。
     */
    public static WordParagraph defaultFirstPage(String title1, String title2) {
        WordParagraph wordParagraph = new WordParagraph();
        wordParagraph.setAlignment(ParagraphAlignment.CENTER);
        wordParagraph.setSpacingBefore(2500);
        wordParagraph.setSpacingBetweenWidth(12d);
        List<WordText> wordTexts = new ArrayList<>();
        WordText wordText = new WordText();
        wordText.setFontFamily(WordDefaultValue.PAGE_1_TITLE_FONT_FAMILY);
        wordText.setFontSize(WordDefaultValue.PAGE_1_TITLE_FONT_SIZE);
        wordText.setBold(true);
        wordText.setBreakType(BreakType.TEXT_WRAPPING);
        wordText.setText(title1);
        wordTexts.add(wordText);

        WordText wordText2 = new WordText();
        wordText2.setText(title2);
        wordText2.setBold(true);
        wordText2.setFontFamily(WordDefaultValue.PAGE_1_TITLE_FONT_FAMILY);
        wordText2.setFontSize(WordDefaultValue.PAGE_1_TITLE_FONT_SIZE);
        wordText2.setBreakType(BreakType.PAGE);
        wordTexts.add(wordText2);

        wordParagraph.setTexts(wordTexts);
        return wordParagraph;
    }

    /**
     * 页面标题
     * @param texts .
     * @return .
     */
    public static List<WordParagraph> defaultTitleParagraphText(String... texts) {
        List<WordParagraph> wordParagraphs = new ArrayList<>();
        for (String text: texts) {
            WordText wordText = new WordText();
            wordText.setText(text);
            wordText.setFontSize(WordDefaultValue.PAGE_2_TITLE_FONT_SIZE);
            wordText.setFontFamily(WordDefaultValue.PAGE_2_TITLE_FONT_FAMILY);
            WordParagraph wordParagraph = new WordParagraph();
            wordParagraph.setSpacingBetweenWidth(1.0);
            wordParagraph.setAlignment(ParagraphAlignment.CENTER);
            wordParagraph.setTexts(Collections.singletonList(wordText));
            wordParagraphs.add(wordParagraph);
        }
        return wordParagraphs;
    }

    /**
     * 文本内容
     * @param text .
     * @return .
     */
    public static WordParagraph defaultParagraphText(String text) {
        WordParagraph wordParagraph = new WordParagraph();
        wordParagraph.setIndentationFirstLine((int) WordDefaultValue.TEXT_FONT_SIZE * 2 * 20);
        wordParagraph.setSpacingBetweenWidth(1.5);
        WordText wordText = new WordText();
        wordText.setText(text);
        wordText.setFontSize(WordDefaultValue.TEXT_FONT_SIZE);
        wordText.setFontFamily(WordDefaultValue.TEXT_FONT_FAMILY);
        wordParagraph.setTexts(Collections.singletonList(wordText));
        return wordParagraph;
    }

    /**
     * 空行
     * @return 。
     */
    public static WordParagraph emptyLine() {
        WordParagraph wordParagraph = new WordParagraph();
        WordText wordText = new WordText();
        wordText.setText("");
        wordParagraph.setTexts(Collections.singletonList(wordText));
        return wordParagraph;
    }

    /**
     * 标题1
     * @param text 。
     * @return 。
     */
    public static WordParagraph defaultHeading1(String text) {
        WordText wordText = new WordText();
        wordText.setText(text);
        wordText.setBold(true);
        wordText.setFontSize(WordDefaultValue.HEADING1_FONT_SIZE);
        wordText.setFontFamily(WordDefaultValue.HEADING1_FONT_FAMILY);
        return wordTitleText("2", wordText);
    }

    /**
     * 标题2
     * @param text 。
     * @return 。
     */
    public static WordParagraph defaultHeading2(String text) {
        WordText wordText = new WordText();
        wordText.setText(text);
        wordText.setFontSize(WordDefaultValue.HEADING2_FONT_SIZE);
        wordText.setFontFamily(WordDefaultValue.HEADING2_FONT_FAMILY);
        return wordTitleText("3", wordText);
    }

    /**
     * 标题
     * @param style 。
     * @param wordText 。
     * @return 。
     */
    public static WordParagraph wordTitleText(String style, WordText wordText) {
        WordParagraph wordParagraph = new WordParagraph();
        wordParagraph.setStyle(style);
        wordParagraph.setSpacingAfter(200);
        wordParagraph.setTexts(Collections.singletonList(wordText));
        return wordParagraph;
    }


    @Override
    public WordElementType getElementType() {
        return WordElementType.TEXT;
    }

    @Override
    public void addElement(XWPFDocument document) {
        XWPFParagraph p = paragraphBuild(document);
        for (WordText wordText: texts) {
            XWPFRun r = p.createRun();
            wordText.textBuild(r);
//            WordHyperLink wordHyperLink = wordText.getHyperLink();
//            if (wordHyperLink != null) {
//                XWPFHyperlinkRun hyperlink = p.insertNewHyperlinkRun(index, wordHyperLink.getUrl());
//                if (wordHyperLink.getUnderline() != null) {
//                    hyperlink.setUnderline(wordHyperLink.getUnderline());
//                }
//                if (wordHyperLink.getColor() != null) {
//                    hyperlink.setColor(wordHyperLink.getColor());
//                }
//                if (wordHyperLink.getText() != null) {
//                    hyperlink.setText(wordHyperLink.getText());
//                }
//            }

        }
    }

    private XWPFParagraph paragraphBuild(XWPFDocument document) {
        XWPFParagraph p = document.createParagraph();
        if (alignment != null) {
            p.setAlignment(alignment);
        }
        if (borderBottom != null) {
            p.setBorderBottom(borderBottom);
        }
        if (verticalAlignment != null) {
            p.setVerticalAlignment(verticalAlignment);
        }
        if (spacingBetweenWidth != null) {
            if (spacingBetweenRule != null) {
                p.setSpacingBetween(spacingBetweenWidth, spacingBetweenRule);
            } else {
                p.setSpacingBetween(spacingBetweenWidth);
            }
        }
        if (spacingBefore != null) {
            p.setSpacingBefore(spacingBefore);
        }
        if (spacingAfter != null) {
            p.setSpacingAfter(spacingAfter);
        }
        if (indentationFirstLine != null) {
            p.setIndentationFirstLine(indentationFirstLine);
        }
        if (style != null) {
            p.setStyle(style);
        }
        return p;
    }
}
